/**
 * 
 */
Ext.namespace("Ext.lams");

Ext.lams.TreeComboBox = Ext.extend(Ext.form.ComboBox, {
	constructor : function(config) {
		Ext.apply(this, config || {});
		this.treeId = Ext.id() + '-tree';
		this.maxHeight = arguments[0].maxHeight || arguments[0].height
				|| this.maxHeight;
		this.tpl = new Ext.Template('<tpl for="."><div style="height:'
				+ this.maxHeight + 'px"><div id="' + this.treeId
				+ '"></div></div></tpl>');
		this.store = new Ext.data.SimpleStore({
					fields : [],
					data : [[]]
				});
		this.selectedClass = '';
		this.mode = 'local';
		this.triggerAction = 'all';
		this.onSelect = Ext.emptyFn;
		this.editable = false;
		this.beforeBlur = Ext.emptyFn;
		Ext.lams.TreeComboBox.superclass.constructor.call(this);
		this.addEvents('afterchange');
		this.addEvents('select');
		this.addEvents('beforeselect');
	},
	initComponent : function() {
        
		var root = new Ext.tree.AsyncTreeNode({
					text : this.rootText,
					id : this.rootId,
					expanded : true
				});
		var tree = new Ext.tree.TreePanel({
					border : false,
					root : root,
					rootVisible : false,
					autoScroll : true,
					animate:true,
					baseparams:'id',
					loader : new Ext.tree.TreeLoader({
								dataUrl : this.treeUrl,
								clearOnLoad : true
							}),
					containerScroll : false,
					listeners : {
						scope : this,
						click : function(node) {
							this.setValue(node);
							this.collapse();
						}
					}
				});
		this.tree = tree;
      
		Ext.lams.TreeComboBox.superclass.initComponent.call(this);
        
      //  this.expand();
	},
	expand : function() {
		Ext.lams.TreeComboBox.superclass.expand.call(this);
		if (this.tree.rendered) {
			return;
		}
		Ext.apply(this.tree, {
					height : this.maxHeight,
					width : 150,//(this.listWidth || this.width - (Ext.isIE ? 3 : 0))- 2,
					border : false,
					autoScroll : true
				});
		if (this.tree.xtype) {
			this.tree = Ext.ComponentMgr.create(this.tree, this.tree.xtype);
		}
		this.tree.render(this.treeId);

		var root = this.tree.getRootNode();
		if (!root.isLoaded())
			root.reload();

		this.tree.on('click', function(node) {
					var selModel = this.selectNodeModel;
					var isLeaf = node.isLeaf();

					if ((node == root) && selModel != 'all') {
						return;
					} else if (selModel == 'folder' && isLeaf) {
						return;
					} else if (selModel == 'leaf' && !isLeaf) {
						return;
					}

					var oldNode = this.getNode();
					if (this.fireEvent('beforeselect', this, node, oldNode) !== false) {
						this.setValue(node);
						this.collapse();

						this.fireEvent('select', this, node, oldNode);
						(oldNode !== node) ? this.fireEvent('afterchange',
								this, node, oldNode) : '';
					}
				}, this);
	},
	setValue : function(node) {
		this.node = node;
		var text = node.text;
		this.lastSelectionText = text;
        this.setRawValue(text);
		if (this.hiddenField) {
			this.hiddenField.value = node.id;
		}
		Ext.lams.TreeComboBox.superclass.setValue.call(this, text);
		this.value = node.id;
	},
	getValue : function() {
		return typeof this.value != 'undefined' ? this.value : '';
	},

	getNode : function() {
		return this.node;
	}
});
Ext.reg("treecombobox", Ext.lams.TreeComboBox);
